/*
package com.bootdo.system.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.FileInfoDao;
import com.bootdo.system.dao.SetDao;
import com.bootdo.system.domain.FileInfoDO;
import com.bootdo.system.domain.SetDO;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class SynchoTaskFor {

    @Autowired
    private SetDao setDao;

    @Autowired
    private BootdoConfig bootdoConfig;

    @Autowired
    private FileInfoDao fileInfoDao;

    @PostConstruct
    public void run() {
        Timer timer = new Timer();
        //表示在0秒之后开始执行，并且每15秒执行一次
        //timer.schedule(new configureTasks(),0,15000);
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("-----------定时器任务5秒执行一次------------" + new Date());
                //获取配置文件地址
                String synchroPath = bootdoConfig.getSynchroPath();
                //读取配置文件里面的文件信息
                Properties properties = new Properties();
                // 使用InPutStream流读取properties文件
                BufferedReader bufferedReader = null;
                try {
                    bufferedReader = new BufferedReader(new FileReader(synchroPath));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                try {
                    properties.load(bufferedReader);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //获取同步服务器ip地址
                String synchroIp = properties.getProperty("synchroIp");
                //获取同步服务器port端口号
                String synchroPort = properties.getProperty("synchroPort");
                //判断目标服务器是否正常运行
                if(StringUtils.isNotEmpty(synchroIp) && StringUtils.isNotEmpty(synchroPort)) {
                    boolean flag = isConnect(synchroIp,Integer.parseInt(synchroPort));
                    String logPath = bootdoConfig.getLogPath();
                    String logLocation = bootdoConfig.getLogLocation();
                    SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
                    if(flag) {
                        try {
                            //目标服务器正常运行
                            //获取同步服务器ip地址
                            DriverManagerDataSource dataSource=new DriverManagerDataSource();
                            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
                            dataSource.setUrl("jdbc:mysql://" + bootdoConfig.getLocalDataBaseIp() + ":" + bootdoConfig.getRemoteDataBasePort() + "/baidujiex");
                            dataSource.setUsername(bootdoConfig.getRemoteDataBaseUser());
                            dataSource.setPassword(bootdoConfig.getRemoteDataBasePassword());
                            JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
                            //数据源信息同步
                            //获取没有同步的文件
                            String sql = "select * from baidujiex.file_info";
                            List<FileInfoDO> list = jdbcTemplate.query(sql, new RowMapper() {
                                @Override
                                public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                                    FileInfoDO fileInfoDO = new FileInfoDO();
                                    fileInfoDO.setUuid(resultSet.getString("uuid"));
                                    fileInfoDO.setFilename(resultSet.getString("filename"));
                                    fileInfoDO.setFiletype(resultSet.getString("filetype"));
                                    fileInfoDO.setId(resultSet.getInt("id"));
                                    return fileInfoDO;
                                }
                            });
                            if(!CollectionUtils.isEmpty(list)) {
                                System.out.println("-----------定时器任务开始执行，哈哈哈哈哈哈哈哈哈哈或或或或或或或------------" + new Date());
                                //获取文件保存根目录地址
                                SetDO setDO = setDao.get("diskSave");
                                if(null != setDO) {
                                    String rootPath = setDO.getSetValue();
                                    for (FileInfoDO data : list) {
                                        //原文件实际位置
                                        String actualPath = rootPath + File.separator + "ENCRY_" + data.getUuid() + data.getFilename() + "." + data.getFiletype();
                                        File file1 = new File(actualPath);
                                        if (file1.exists()) {
                                            FileInputStream fileInputStream = null;
                                            try {
                                                fileInputStream = new FileInputStream(file1);
                                            } catch (FileNotFoundException e) {
                                                e.printStackTrace();
                                            }
                                            try {
                                                */
/*MultipartFile file = new MockMultipartFile(file1.getName(), file1.getName(),
                                                        ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);*//*

                                                //同步服务器访问地址
                                                String ipPath = "http://" + synchroIp + ":" + synchroPort + "/sys/userFile/synchroFile";
                                                InputStream inputStream = new FileInputStream(actualPath);

                                                Date d= new Date();
                                                String str = sdf.format(d);
                                                String logData1 = "------文件开始同步;开始时间为" + str + "--------";
                                                //文件同步日志记录
                                                FileUtil.writeFile(logData1, logPath, logLocation);

                                                String content = "";
                                                CloseableHttpClient httpClient = HttpClientBuilder.create().build();
                                                HttpPost httpPost = new HttpPost(ipPath);
                                                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                                                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//解决返回中文乱码问题
                                                builder.setCharset(Charset.forName("UTF-8")); //此处踩坑，转发出去的filename依然为乱码
                                                ContentType strContent = ContentType.create("text/plain",Charset.forName("UTF-8"));
                                                builder.addTextBody("rootPath", rootPath,strContent);
                                                builder.addTextBody("actualPath", actualPath,strContent);
                                                builder.addTextBody("fileId",data.getId().toString(),strContent);
                                                builder.addTextBody("uuid",data.getUuid(),strContent);
                                                builder.addTextBody("fileName",data.getFilename(),strContent);
                                                builder.addTextBody("fileType",data.getFiletype(),strContent);
                                                try {
                                                    builder.addBinaryBody("file", inputStream, ContentType.MULTIPART_FORM_DATA, file1.getName());
                                                    HttpEntity httpEntity = builder.build();
                                                    httpPost.setEntity(httpEntity);
                                                    HttpResponse httpResponse = httpClient.execute(httpPost);
                                                    HttpEntity responseEntity = httpResponse.getEntity();
                                                    if(null != responseEntity) {
                                                        content = EntityUtils.toString(responseEntity, "UTF-8");
                                                    }
                                                }catch (IOException e) {
                                                    e.printStackTrace();
                                                }finally {
                                                    try {
                                                        httpClient.close();
                                                    } catch (IOException e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                                String logData2 = "";
                                                Date d1 = new Date();
                                                String str1 = sdf.format(d1);
                                                JSONObject jsonObject = new JSONObject();
                                                try {
                                                    jsonObject = JSON.parseObject(content);
                                                    if(null != jsonObject) {
                                                        String code = jsonObject.getString("code");
                                                        if(StringUtils.equals("0",code)) {
                                                            logData2 = "------文件同步成功;同步时间为" + str1 + ";服务器的ip为" + synchroIp + ";端口号为" + synchroPort +
                                                                    ";请求url为" + ipPath + ";文件名为" + data.getFilename() + "." + data.getFiletype() + "--------";
                                                            //文件同步日志记录
                                                            FileUtil.writeFile(logData2, logPath, logLocation);
                                                            //文件同步成功，删除文件
                                                            String sql1 = "delete from baidujiex.file_info where id = ?";
                                                            jdbcTemplate.update(sql1,data.getId());

                                                            //正常同步成功
                                                            //同步时间
                                                            data.setAsynctime(new Date());
                                                            data.setStatus("1");
                                                            fileInfoDao.updateByUuid(data);

                                                            //计算同步时间
                                                            // 执行时长(毫秒)
                                                            long time = d1.getTime() - d.getTime();
                                                            //保留两位小数
                                                            double f1 = new BigDecimal((float)time/1000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                                            String logData4 = "------文件同步完成;所需时间为" + f1 + "秒--------";
                                                            //文件同步日志记录
                                                            FileUtil.writeFile(logData4, logPath, logLocation);
                                                        }else {
                                                            logData2 = "------文件同步失败;同步时间为" + str1 + ";服务器的ip为" + synchroIp + ";端口号为" + synchroPort +
                                                                    ";请求url为" + ipPath + ";文件名为" + data.getFilename() + "." + data.getFiletype() + "--------";
                                                            //文件同步日志记录
                                                            FileUtil.writeFile(logData2, logPath, logLocation);
                                                        }
                                                    }else {
                                                        logData2 = "------文件同步失败;同步时间为" + str1 + ";服务器的ip为" + synchroIp + ";端口号为" + synchroPort +
                                                                ";请求url为" + ipPath + ";文件名为" + data.getFilename() + "." + data.getFiletype() + "--------";
                                                        //文件同步日志记录
                                                        FileUtil.writeFile(logData2, logPath, logLocation);
                                                    }
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                    logData2 = "------文件同步失败;同步时间为" + str1 + ";服务器的ip为" + synchroIp + ";端口号为" + synchroPort +
                                                            ";请求url为" + ipPath + ";文件名为" + data.getFilename() + "." + data.getFiletype() + "--------";
                                                    //文件同步日志记录
                                                    FileUtil.writeFile(logData2, logPath, logLocation);
                                                }
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        }else {
                                            //文件不存在，直接删除文件
                                            String sql1 = "delete from baidujiex.file_info where id = ?";
                                            jdbcTemplate.update(sql1,data.getId());
                                        }
                                    }
                                }
                            }
                        }catch (Exception e) {
                            System.out.println("------数据库读取异常-------" + new Date() + e);
                            //文件同步日志记录
                            Date d1 = new Date();
                            String str2 = sdf.format(d1);
                            String logData3 = "------数据库读取异常-------,异常时间为" + str2 + ",异常信息为" + e + "------";
                            //文件同步日志记录
                            FileUtil.writeFile(logData3, logPath, logLocation);
                        }
                    }else {
                        //文件同步日志记录
                        Date d1 = new Date();
                        String str2 = sdf.format(d1);
                        String logData3 = "------目标服务器宕机，无法连接目标服务器,宕机时间为" + str2 + "------";
                        //文件同步日志记录
                        FileUtil.writeFile(logData3, logPath, logLocation);
                    }
                }
            }
        }, 0,5000);
    }

    */
/*
     * 判断目标服务器是否正常运行
     * @param host
     * @param port
     * @return
     **//*

    public static boolean isConnect(String host, int port){
        Socket socket = new Socket();
        try{
            socket.connect(new InetSocketAddress(host, port));
        }catch (IOException e) {
            return false;
        }finally{
            try{
                socket.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

}
*/
